package com.tangcco.lockwell.controller.student;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.config.RestTemplateConfig;
import com.tangcco.lockwell.domain.TranslateBackupsInfo;
import com.tangcco.lockwell.domain.dto.english.TranslateDto;
import com.tangcco.lockwell.domain.dto.english.TranslateResponseDto;
import com.tangcco.lockwell.service.TranslateBackupsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单词翻译接口
 *
 * @author yanxinping
 * @date 2025-01-03 14:07
 */
@RestController
@RequestMapping("/api/student/translate")
@Slf4j
public class TranslateController {

    @Resource
    private RestTemplate transClient;
    @Resource
    private TranslateBackupsInfoService translateBackupsInfoService;

    @RequestMapping("/word")
    //q=emit&q=emit&from=en&to=zh&appid=20250103002244236&salt=0.5276635059338433&sign=96ff2008570eb3fc157a440bd690c6cd
    public RestResponse<JsonNode> translate(@RequestBody TranslateDto translateDto) {
        try {
            String url = RestTemplateConfig.transUrl+"?q="+translateDto.getQ()+"&from="+translateDto.getFrom()+"&to="+translateDto.getTo()
                    +"&appid="+translateDto.getAppid()+"&salt="+translateDto.getSalt()+"&sign="+translateDto.getSign();
            JsonNode node = transClient.getForObject(url, JsonNode.class);
            return RestResponse.ok(node);
        } catch (RestClientException e) {
            log.error("单词翻译失败: ", e);
            return RestResponse.ok();
        }
    }
    @RequestMapping("/paragraph")
    //q=emit&q=emit&from=en&to=zh&appid=20250103002244236&salt=0.5276635059338433&sign=96ff2008570eb3fc157a440bd690c6cd
    public RestResponse<JsonNode> paragraph(@RequestBody TranslateDto translateDto) {
        try {
            //先查询数据库是否已经有备份
            TranslateBackupsInfo translateBackupsInfo = translateBackupsInfoService.selectByArticleIdAndParagraph(translateDto.getArticleId(),translateDto.getIndex());
            if(translateBackupsInfo!=null){
                TranslateResponseDto translateResponseDto = new TranslateResponseDto();
                translateResponseDto.setFrom("en");
                translateResponseDto.setTo("zh");
                Map<String, String> map = new HashMap<>();
                map.put("dst",translateBackupsInfo.getChineseText());
                List<Map<String, String>> list = new ArrayList<>();
                list.add(map);
                translateResponseDto.setTrans_result(list);
                ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonNode = mapper.valueToTree(translateResponseDto);
                return RestResponse.ok(jsonNode);
            }else{
                String url = RestTemplateConfig.transUrl+"?q="+translateDto.getQ()+"&from="+translateDto.getFrom()+"&to="+translateDto.getTo()
                        +"&appid="+translateDto.getAppid()+"&salt="+translateDto.getSalt()+"&sign="+translateDto.getSign();
                JsonNode node = transClient.getForObject(url, JsonNode.class);
                //如果不存在，则查询，后存入库
                TranslateBackupsInfo newInfo = new TranslateBackupsInfo();
                newInfo.setArticleId(translateDto.getArticleId());
                newInfo.setParagraph(translateDto.getIndex());
                newInfo.setChineseText(node.get("trans_result").get(0).get("dst").asText());
                newInfo.setEnglishText(translateDto.getQ());
                translateBackupsInfoService.insertSelective(newInfo);
                return RestResponse.ok(node);
            }
        } catch (RestClientException e) {
            log.error("单词翻译失败: ", e);
            return RestResponse.ok();
        }
    }
}

